﻿using System.Net;
using System.Net.Sockets;
using System.Text;

namespace airwolf
{
    public static class UDP
    {
        public static string Receive(string IP, int Port)
        {
            byte[] rec;

            using (UdpClient udpClient = new UdpClient())
            {
                udpClient.Connect(IP, Port);
                Log.LogMessage("SSDP connected and waiting to respond.");
                IPAddress ip = IPAddress.Parse(IP);
                IPEndPoint ep = new IPEndPoint(ip, Port);
                rec = udpClient.Receive(ref ep);
                Log.LogMessage("Received {0} bytes", rec.Length);
            }

            return ASCIIEncoding.ASCII.GetString(rec);
        }

        public static void Send(string IP, int Port, byte[] Data)
        {
            using (UdpClient udpClient = new UdpClient())
            {
                //udpClient.Connect(IP, Port);
                Log.LogMessage("SSDP connected and waiting to respond.");
                //IPAddress ip = IPAddress.Parse(IP);
                //IPEndPoint ep = new IPEndPoint(ip, Port);
                udpClient.Send(Data, Data.Length, IP, Port);
                Log.LogMessage("Sent HTTP OK");
            }
        }
    }
}
